package com.ccnf.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.ccnf.core.bean.Response;
import com.ccnf.model.ao.PassToken;
import com.ccnf.model.entity.Authorization;
import com.ccnf.model.entity.Invitation;
import com.ccnf.service.InvitationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/import")
@Api(value = "ImportController", description = "导入相关API")
public class ImportController extends BaseController {
    @Autowired
    private InvitationService invitationService;

    @ApiOperation("批量导入邀请函数据（excel）")
    @PostMapping("invitation/excel")
    @PassToken
    public Response excelImportInvi(MultipartFile file, Long expoId) {
        ImportParams params = new ImportParams();
        try {
            List<Invitation> invitationList = ExcelImportUtil.importExcel(file.getInputStream(), Invitation.class, params);
            invitationService.batchImportInvi(expoId, invitationList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.success();
    }

    @ApiOperation("下载邀请函导入模板")
    @GetMapping("download/invitation/template")
    @PassToken
    public void downloadInvi(HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Invitation.class, new ArrayList<>());
        // 1. 设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        // 2. 设置响应头，把文件名字设置好
        response.setHeader("Content-Disposition", "attachment; filename=" + "invitation-template.xls");
        workbook.write(response.getOutputStream());
    }

    @ApiOperation("录入授权信息")
    @PostMapping("auth/excel")
    @PassToken
    public Response importAuthExcel(MultipartFile file, Long expoId) {
        ImportParams params = new ImportParams();
        try {
            List<Authorization> authorizationList = ExcelImportUtil.importExcel(file.getInputStream(), Authorization.class, params);
            invitationService.batchImportAuth(expoId, authorizationList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.success();
    }

    @ApiOperation("下载授权导入模板")
    @GetMapping("download/auth/template")
    @PassToken
    public void downloadAuth(HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Authorization.class, new ArrayList<>());
        // 1. 设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        // 2. 设置响应头，把文件名字设置好
        response.setHeader("Content-Disposition", "attachment; filename=" + "authorization-template.xls");
        workbook.write(response.getOutputStream());
    }
}